package org.sy.lab.多线程基础._3_同步辅助类._3_CyclicBarrier同步屏障;

import java.util.concurrent.TimeUnit;

//CyclicBarrier允许两个或者多个线程在某个点上进行同步
//可以传入另一个Runnable对象作为初始化参数，当所有线程都到达集合点后，CyclicBarrier类将这个Runnable对象作为线程优先去执行
//比CountDownLatch功能强大，支持用reset()方法重置，getNumberWaiting()可以获得阻塞的线程数量，isBroken()判断是否被中断
//例子里5个人参与众筹，在5个人参加后众筹成功，生产完产品，5个人结束众筹
public class Main {

	public static void main(String[] args) {
		
		Product product = new Product();
		Crowdfunding crowdfunding = new Crowdfunding(5, product);
		
		MyThread myThread = new MyThread(crowdfunding);
		for (int i = 1; i <= 5; i++) {
			Thread thread = new Thread(myThread, "客户" + i);
			thread.start();
			try {
				TimeUnit.SECONDS.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
